package org.wangle.starter.lock.redis;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scripting.support.ResourceScriptSource;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.reflect.Field;

@Configuration
public class RedisConfiguration {

    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<String ,Object> redisTemplate(){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
        return redisTemplate;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplateForLock(){
        // 创建RedisTemplate<String, Object>对象
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置连接工厂
        template.setConnectionFactory(redisConnectionFactory);
        // 定义Jackson2JsonRedisSerializer序列化对象
        Jackson2JsonRedisSerializer<Object> jacksonSeial = new Jackson2JsonRedisSerializer<>(Object.class);
        StringRedisSerializer stringSerial = new StringRedisSerializer();
        // redis key 序列化方式使用stringSerial
        template.setKeySerializer(stringSerial);
        // redis value 序列化方式使用jackson
        template.setValueSerializer(stringSerial);
        // redis hash key 序列化方式使用stringSerial
        template.setHashKeySerializer(stringSerial);
        // redis hash value 序列化方式使用jackson
        template.setHashValueSerializer(stringSerial);
        template.afterPropertiesSet();
        return template;
    }


    /**
     * 初始化redis飞重入锁的redisTemplate
     */
    @PostConstruct
    public void init(){
       try {
           ResourceScriptSource resourceScriptSource = new ResourceScriptSource(new ClassPathResource("script/getLock.lua"));
           String getLock = resourceScriptSource.getScriptAsString();
           ResourceScriptSource resourceScriptSource2 = new ResourceScriptSource(new ClassPathResource("script/unLock.lua"));
           String getUnlock = resourceScriptSource2.getScriptAsString();

           RedisUtils redisUtils = new RedisUtils();
           Class<? extends RedisUtils> aClass1 = redisUtils.getClass();
           Field redisTemplate1 = aClass1.getDeclaredField("redisTemplate");
           redisTemplate1.setAccessible(true);
           redisTemplate1.set(redisUtils, redisTemplate());

           DoLua noReentrantLock = new DoLua();
           Class<?> aClass = noReentrantLock.getClass();
           Field redisTemplate = aClass.getDeclaredField("redisTemplate");
           Field lockScript = aClass.getDeclaredField("lockScript");
           Field unLockScript = aClass.getDeclaredField("unLockScript");
           redisTemplate.setAccessible(true);
           lockScript.setAccessible(true);
           unLockScript.setAccessible(true);

           redisTemplate.set(noReentrantLock, redisTemplateForLock());
           lockScript.set(noReentrantLock, getLock);
           unLockScript.set(noReentrantLock, getUnlock);



       }catch (Exception e){
           e.printStackTrace();
       }

    }
}
